package com.puman.ribbonconsumer;


import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.puman.ribbonconsumer.model.User;
import com.puman.ribbonconsumer.service.HelloService;
import com.puman.ribbonconsumer.service.UserGetCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

@RestController
public class ConsumerController {

    @Autowired
    HelloService helloService;
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String helloConsumer(Long id){
        HystrixRequestContext.initializeContext();
        System.out.println("我进来了");
        User u = helloService.getUserById(id);
        User u2 = helloService.getUserById(id);
        return u.toString()+u2.toString();
    }
    @RequestMapping(value = "/ribbon-consumer-async",method = RequestMethod.GET)
    public String helloConsumerAsync(Long id) throws  Exception{
        System.out.println("我进来了");
        Future<User> user =  helloService.getUserByIdAsync(id+"");
        System.out.println("我进来了1");
        return user.get().toString();
    }
//    @RequestMapping(value = "/hello-param",method = RequestMethod.GET)
//    public String helloParam(){
//        return restTemplate.getForEntity("http://HELLO-SERVICE/helloParam?name={1}",String.class," yanfaze").getBody();
//
//    }
//    @RequestMapping(value = "/hello-params",method = RequestMethod.GET)
//    public String helloParams(){
//        return restTemplate.getForEntity("http://HELLO-SERVICE/helloParams?name={1}&age={2}",String.class," yanfaze","26").getBody();
//
//    }
//    @RequestMapping(value = "/hello-params-map",method = RequestMethod.GET)
//    public String helloParamsMap(){
//        Map<String,String> map = new HashMap<>();
//        map.put("name","闫发泽");
//        map.put("age","26");
//        return restTemplate.getForEntity("http://HELLO-SERVICE/helloParams?name={name}&age={age}",String.class,map).getBody();
//
//    }
//    @RequestMapping(value = "/hello-params-uri",method = RequestMethod.GET)
//    public String helloParamsURI(){
//        Map<String,String> map = new HashMap<>();
//        map.put("name","闫发泽");
//        map.put("age","26");
//        UriComponents uriComponents= UriComponentsBuilder.fromUriString(
//                "http://HELLO-SERVICE/helloParams?name={name}&age={age}").build()
//                .expand(map).encode();
//        URI uri=uriComponents.toUri();
//        return restTemplate.getForEntity(uri,String.class).getBody();
//    }
//    @RequestMapping(value = "/hello-object",method = RequestMethod.GET)
//    public String helloObject(){
//        Map<String,String> map = new HashMap<>();
//        map.put("name","闫发泽");
//        map.put("age","26");
//        UriComponents uriComponents= UriComponentsBuilder.fromUriString(
//                "http://HELLO-SERVICE/helloParams?name={name}&age={age}").build()
//                .expand(map).encode();
//        URI uri=uriComponents.toUri();
//        return restTemplate.getForObject(uri,String.class);
//    }
//
//    @RequestMapping(value = "/postConsumer",method = RequestMethod.GET)
//    public String postConsumer(){
//        Map<String,Object> map = new HashMap<>();
//        map.put("name","闫发泽");
//        map.put("age",26);
//        return restTemplate.postForEntity("http://HELLO-SERVICE/postParam",map,String.class).getBody();
//
//    }
//    @RequestMapping(value = "/postURI",method = RequestMethod.GET)
//    public String postURI(){
//        Map<String,Object> map = new HashMap<>();
//        map.put("name","闫发泽");
//        map.put("age",26);
//        URI uri = restTemplate.postForLocation("http://HELLO-SERVICE/postParam",map);
//        System.out.println(uri);
//        return "hello";
//    }
}

